<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img src="../assets/95D584521E5E5CB43A6C8F0924995C2E.jpg" alt />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :label-position="labelPosition"
        :rules="loginFormRules"
        label-width="70px"
        class="login-form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-s-custom"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password" label="密码">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-radio v-model="radio" label="1">管理员</el-radio>
          <el-radio v-model="radio" label="2">用户</el-radio>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" size="small" @click="login">登录</el-button>
          <el-button type="info" size="small">注册</el-button>
          <el-button type="warning" size="small" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      labelPosition: 'left',
      // 登录表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 登录表单验证规则对象
      loginFormRules: {
        // 用户名验证规则
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ],
        // 密码验证规则
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      },
      radio: 1
    }
  },
  methods: {
    // 重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },

    // 登录前验证
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        // valid为false则不发起请求
        if (!valid) {
          return
        }
        // 登录请求(方法：post，路径：login)
        // 解构赋值data得到真实的访问数据
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败，请检查用户名或密码！')
        }
        this.$message.success('登录成功！')
        // 将token保存到sessionStorage中
        window.sessionStorage.setItem('token', res.data.token)
        // 编程式导航对象跳转到 /home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
  overflow: hidden;
  .login-box {
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 5px;
    /* 盒子能在页面上位移 */
    position: absolute;
    left: 50%;
    top: 50%;
    /* 横轴上移动-50%, 纵轴上移动-50% */
    transform: translate(-50%, -50%);
    .avatar-box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 5px;
      /* 加阴影，向外扩散10像素 */
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      /* 再横向移动盒子自身的-50% ，纵向移动-50%*/
      transform: translate(-50%, -40%);
      background-color: #fff;
      /* 使img填充满父盒子 */
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 50px 0 20px;
      /* form表单默认box-sizing为content-box，设置为border-box即可实现padding值 */
      box-sizing: border-box;
      .btns {
        /* 设置为弹性盒子 */
        display: flex;
        /* 尾部对齐 */
        justify-content: flex-end;
      }
    }
  }
}
</style>
